package com.guijq.codesegment.hutool.tree;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.lang.tree.Tree;
import cn.hutool.core.lang.tree.TreeNode;
import cn.hutool.core.lang.tree.TreeNodeConfig;
import cn.hutool.core.lang.tree.TreeUtil;
import cn.hutool.core.lang.tree.parser.DefaultNodeParser;

import java.util.List;

/**
 * @Author timyag
 * @Date 2021/5/19 17:30
 * @Version 1.0
 */
public class TreeLearn {

    public static void main(String[] args) {
        TreeNode a = new TreeNode<>("1", "0", "系统管理", 5);
        EntityBo bo = new EntityBo(20, "高中");

        a.setExtra(BeanUtil.beanToMap(bo));
        List<TreeNode<String>> nodeList = CollUtil.newArrayList();

        nodeList.add(a);
        nodeList.add(new TreeNode<>("11", "1", "用户管理", 222222));
        nodeList.add(new TreeNode<>("111", "11", "用户添加", 0));
        nodeList.add(new TreeNode<>("2", "0", "店铺管理", 1));
        nodeList.add(new TreeNode<>("21", "2", "商品管理", 44));
        nodeList.add(new TreeNode<>("221", "2", "商品管理2", 2));

        // 0表示最顶层的id是0
        List<Tree<String>> treeList = TreeUtil.build(nodeList, "0");

        //
        //配置
        TreeNodeConfig treeNodeConfig = new TreeNodeConfig();
        // 自定义属性名 都要默认值的
        treeNodeConfig.setWeightKey("order");
        treeNodeConfig.setIdKey("rid");
        // 最大递归深度
//        treeNodeConfig.setDeep(3);

        //转换器 自定义转换器
//        List<Tree<String>> treeNodes = TreeUtil.build(nodeList, "0", treeNodeConfig,
//                (treeNode, tree) -> {
//                    tree.setId(treeNode.getId());
//                    tree.setParentId(treeNode.getParentId());
//                    tree.setWeight(treeNode.getWeight());
//                    tree.setName(treeNode.getName());
//                    // 扩展属性 ...
//                    tree.putExtra("extra", treeNode.getExtra());
//                    tree.putExtra("extraField", 666);
//                    tree.putExtra("other", new Object());
//                });

        // 转换器 -默认转换器
        List<Tree<String>> treeNodes = TreeUtil.build(nodeList, "0", treeNodeConfig,
                new DefaultNodeParser<>());

        System.out.println(treeNodes);
        System.out.println(treeNodes.get(0).getId());

    }
}
